//
//  GSSubscribeViewController_Add.m
//  GameSky
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSSubscribeViewController_Add.h"
#import "GSSubscribeViewController_Add_Topic_Details.h"
#import "GSSubscribeViewController_Add_Topic.h"
#import "GSSubscribeViewController_Add_Mine.h"
#import "GSSearchViewController.h"
#import "GWAppUIContentViewNavigationBar.h"

@interface GSSubscribeViewController_Add () <UIScrollViewDelegate,GSSubscribeViewController_Add_MineDelegate>
// 展示视图
@property (nonatomic, strong) UIScrollView *scrollView;
// navigationBar
@property (nonatomic, strong) GWAppUIContentViewNavigationBar *Bar;
// 展示视图---热门
@property (nonatomic, strong) GSSubscribeViewController_Add_Topic *hotVC;
// 展示视图---全部
@property (nonatomic, strong) GSSubscribeViewController_Add_Topic *allVC;
// 展示视图---我的
@property (nonatomic, strong) GSSubscribeViewController_Add_Mine *mineVC;

// 保存 刚要拖拽时的点
@property (nonatomic, assign) CGFloat x;

// 保存 已经变大的 标签的tag 值
@property (nonatomic, assign) CGFloat tagNum;

// 保存 要滑动到的controller的 值
@property (nonatomic, assign) CGFloat controllerNum;


// 设置 方法锁, 解决 button点击 改变 scorllView的 偏移量时, didScrollView也执行的造成的bug
@property (nonatomic, assign) NSInteger num;

@end

@implementation GSSubscribeViewController_Add
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NOTIFICATION" object:nil];
}
#pragma mark--------------视图将要出现---------------
// 将要出现时,隐藏 tabBar
- (void)viewWillAppear:(BOOL)animated{
     // 添加自定义 navigationBar
    [self.view addSubview:self.Bar];
    // 隐藏 tabBar
  self.tabBarController.tabBar.hidden = YES;
    
    //当在详情页面 跳转回来,如果做了更改,则应该刷新 界面(更改 订阅状态)
    if (_scrollView) {
  [self refreshByContenOffset:(int)_scrollView.contentOffset.x / kScreenWidth];
    }
    
    
}
#pragma mark--------------视图将要消失---------------
// 将要消失, 取消隐藏
- (void)viewWillDisappear:(BOOL)animated{
    [self.Bar removeFromSuperview];
    self.tabBarController.tabBar.hidden = NO;
}
#pragma mark-------------懒加载bar---------------
- (GWAppUIContentViewNavigationBar *)Bar{
    if (!_Bar) {
        _Bar = [[GWAppUIContentViewNavigationBar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
// block实现
        __weak GSSubscribeViewController_Add *mySelf = self;
        _Bar.block = ^void(NSInteger tag){
            // 实现跳转
            [mySelf skipToAnotherViewController:tag];
        };
// 搜索框 添加方法
        [_Bar.SearchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
// 添加返回按钮 方法
        [_Bar.BackButton addTarget:self action:@selector(backButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _Bar;
}

#pragma mark-------------视图将在完成--------------
- (void)viewDidLoad {
    [super viewDidLoad];
    
// 注册通知, 为了传值
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"NOTIFICATION" object:nil];
    
// 第一次加载是,应是 热门  界面, 将 热门字体变大
    self.Bar.hotButton.titleLabel.font = [UIFont systemFontOfSize:14 * 1.4];
//  保存  热门界面的 对应 的tag值
    self.tagNum = self.Bar.hotButton.tag;
// 初始化 num 属性
    self.num = 0;
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];

// 添加scrollView
    [self addScrollView];
//  scrollView添加子视图
    [self addSubViews];

    
}
- (void)notification:(NSNotification *)notification{
    GSSearchViewController *searchVC = [[GSSearchViewController alloc] init];
    searchVC.number = 1002;
    [self.navigationController pushViewController:searchVC animated:YES];
}
#pragma mark-------------添加scrollView----------------
- (void)addScrollView{
    self.automaticallyAdjustsScrollViewInsets = NO;
// 创建
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    // 夜间模式
    _scrollView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
// 回弹
    _scrollView.bounces = NO;
// 滑动 区域
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 3, _scrollView.frame.origin.y);
// 整页滑动
    _scrollView.pagingEnabled = YES;
// 代理
    _scrollView.delegate = self;
// 轨迹线
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
// 添加 视图
    [self.view addSubview:_scrollView];
}
#pragma mark-----------scrollView添加视图------------
- (void)addSubViews{
// 创建并添加 hot 视图
    _hotVC = [[GSSubscribeViewController_Add_Topic alloc] init];
    _hotVC.typeNumber = @1;
    //_hotVC.view.backgroundColor = [UIColor whiteColor];
    _hotVC.view.frame = CGRectMake(0, 0, kScreenWidth, _scrollView.height);
//  block的实现部分
    __weak GSSubscribeViewController_Add *mySelf = self;
    _hotVC.block = ^void(NSDictionary *dic,BOOL isSubscribe,GSSubscrebe_Add_Topic *model,NSData *imageData){
        [mySelf pushToControllerWithDictionary:dic isSubscribe:isSubscribe model:model data:imageData];
    };
    
    [_scrollView addSubview:_hotVC.view];
// 创建并添加 all 视图
    _allVC = [[GSSubscribeViewController_Add_Topic alloc] init];
    _allVC.typeNumber = @2;
    _allVC.view.backgroundColor = [UIColor whiteColor];
    _allVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, _scrollView.height);
// block实现
    _allVC.block = ^void(NSDictionary *dic,BOOL isSubscribe,GSSubscrebe_Add_Topic *model,NSData *imageData){
        [mySelf pushToControllerWithDictionary:dic isSubscribe:isSubscribe model:model data:imageData];
    };
    
    
    [_scrollView addSubview:_allVC.view];
// 创建并添加 我的 视图
    _mineVC = [[GSSubscribeViewController_Add_Mine alloc] init];
    _mineVC.view.backgroundColor = [UIColor whiteColor];
    _mineVC.view.frame = CGRectMake(kScreenWidth * 2, 0, kScreenWidth, _scrollView.height);
    
    _mineVC.delegate = self;
    
    [_scrollView addSubview:_mineVC.view];
}
#pragma mark--------------我的视图中的协议方法-----------
// GSSubscribeViewController_Add_MineDelegate
- (void)pushToNextControllerWithDic:(NSDictionary *)dic isSubscribe:(BOOL)isSubscribe model:(GSSubscrebe_Add_Topic *)model imageData:(NSData *)imageData{
// 跳转
    [self pushToControllerWithDictionary:dic isSubscribe:isSubscribe model:model data:imageData];
}
#pragma mark--------------跳转方法----------------------
- (void)pushToControllerWithDictionary:(NSDictionary *)dic isSubscribe:(BOOL)isSubscribe model:(GSSubscrebe_Add_Topic *)model data:(NSData *)imageData{
// 创建,并赋属性
    GSSubscribeViewController_Add_Topic_Details *detailsVC = [[GSSubscribeViewController_Add_Topic_Details alloc] init];
    detailsVC.dic = dic;
    detailsVC.isSubscribe = isSubscribe;
    detailsVC.model = model;
    detailsVC.imageData = imageData;
// 跳转
    [self.navigationController pushViewController:detailsVC animated:YES];
}
#pragma mark-------------scrollView协议方法--------------
// 保存 刚要开始 拖拽时的点,  判别滑动方向使用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    _num = 0;
    self.x = scrollView.contentOffset.x;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 设置 方法 锁,  当 点button实现 跳转时,此方法不执行
    if (_num == 0) {
        if ([scrollView isEqual:_scrollView]) {
            int current = (int)_x / kScreenWidth + 0.5;
            CGFloat value = scrollView.contentOffset.x / kScreenWidth;
            // 变化倍数
            CGFloat scale = ABS(value - current) * 0.4;
            UIButton *currentButton = (UIButton *)[self.Bar viewWithTag:100 + current];
            currentButton.titleLabel.font = [UIFont systemFontOfSize:14 * (1.4 - scale)];
            // 判断 方向
            // 向左滑动
            if (_x < scrollView.contentOffset.x) {
                UIButton *towardButton = (UIButton *)[self.Bar viewWithTag:100 + current + 1];
                towardButton.titleLabel.font = [UIFont systemFontOfSize:14 * (1 + scale)];
            }else{
                // 向右滑
                UIButton *towardButton = (UIButton *)[self.Bar viewWithTag:100 + current - 1];
                towardButton.titleLabel.font = [UIFont systemFontOfSize:14 * (1 + scale)];
            }
        }
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat num = scrollView.contentOffset.x / kScreenWidth;
    self.tagNum = (int)100 + num;
    //刷新
    [self refreshByContenOffset:(int)num];
}
#pragma mark-----------------标签点击实现跳转------------
- (void)skipToAnotherViewController:(NSInteger )tag{
    // 判断 是否 与之前点击的一致
    if (tag == _tagNum) {
    }else{
        _num = 1;
        UIButton *currentButton = (UIButton *)[self.Bar viewWithTag:_tagNum];
        currentButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        
        [_scrollView setContentOffset:CGPointMake((tag - 100) * kScreenWidth, 0) animated:YES];
        UIButton *towardButton = (UIButton *)[self.Bar viewWithTag:tag];
      
        towardButton.titleLabel.font = [UIFont systemFontOfSize:14 * 1.4];
        // 当点击的时候,因为设置了 _Num,改变偏移量方法没有 走didScroll, 所以,这里也要改变 页面订阅状态
        [self refreshByContenOffset:(int)tag - 100];
        
        _tagNum = tag;
    }
}
#pragma mark-------------页面转化时,进行刷新---------
- (void)refreshByContenOffset:(int)num{
    if (num == 0) {
        [_hotVC changeSubscribe];
    }else if (num == 1){
        [_allVC changeSubscribe];
    }else{
        [_mineVC changeSubscribe];
    }
}
#pragma mark----------------跳转搜索框-----------------
- (void)searchButtonAction:(UIButton *)sender{
    GSSearchViewController *searchVC = [[GSSearchViewController alloc] init];
    searchVC.number = 1002;
    [self.navigationController pushViewController:searchVC animated:YES];
}
#pragma mark----------------返回按钮实现---------------
- (void)backButtonAction:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark------------------------------------------
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
